package com.suredy.app.porject.charts.ctrl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suredy.app.ctrl.AppBaseCtrl;
import com.suredy.app.porject.constants.BidStatus;
import com.suredy.app.porject.constants.ContractStatus;
import com.suredy.app.porject.constants.ContractType;
import com.suredy.app.porject.constants.FlowStatus;
import com.suredy.app.porject.ctrl.view.ViewRoleCtrl;
import com.suredy.app.porject.model.Bid;
import com.suredy.app.porject.model.Biddinger;
import com.suredy.app.porject.model.Contract;
import com.suredy.app.porject.service.BidSrv;
import com.suredy.app.porject.service.BiddingerSrv;
import com.suredy.app.porject.service.ContractSrv;
import com.suredy.app.porject.service.FlowOfBid2UserSrv;
import com.suredy.app.porject.service.FlowOfContract2UserSrv;
import com.suredy.app.porject.service.ProjectConstants;
import com.suredy.security.entity.OrgEntity;

/**
 * 图标事件
 * 
 * @author VIVID.G
 * @since 2016-3-10
 * @version v0.1
 */
@Controller
@RequestMapping("/chart-event")
public class ChartsEventCtrl extends AppBaseCtrl {

	@Autowired
	private ContractSrv contractSrv;

	@Autowired
	private FlowOfContract2UserSrv contract2UserSrv;

	@Autowired
	private BidSrv bidSrv;

	@Autowired
	private FlowOfBid2UserSrv bid2UserSrv;

	@Autowired
	private ProjectConstants constants;

	@Autowired
	private BiddingerSrv biddingerSrv;

	@RequestMapping(value = "/ht-money-click")
	public String contractAndMoneyList(Contract search, @DateTimeFormat(pattern = "yyyy-M-d") Date start, @DateTimeFormat(pattern = "yyyy-M-d") Date end, Boolean inMoney, Integer page, Integer pageSize, Model model) {
		String view = "app/charts/ht-money-click";

		if (page == null || page < 1)
			page = 1;

		if (pageSize == null || pageSize < 1)
			pageSize = 30;

		DetachedCriteria dc = this.contractSrv.getDc(search);

		dc.add(Restrictions.in("type", ContractType.MAIN_CONTRACT_TYPES));

		if (start != null && end != null) {
			Criterion startC = Restrictions.and(Restrictions.ge("start", start), Restrictions.le("start", end));
			Criterion endC = Restrictions.and(Restrictions.ge("end", start), Restrictions.le("end", end));

			dc.add(Restrictions.or(startC, endC));
		}

		if (Boolean.TRUE.equals(inMoney))
			dc.add(Restrictions.in("type", ContractType.IN_MONEY_CONTRACT_TYPE.toArray()));
		else if (Boolean.TRUE.equals(inMoney))
			dc.add(Restrictions.in("type", ContractType.OUT_MONEY_CONTRACT_TYPE.toArray()));

		List<Contract> data = this.contract2UserSrv.getContractOf(dc, null, true, page, pageSize, FlowStatus.FS2001_CONTRACT_CREATED, FlowStatus.FS2002_CONTRACT_STARTED, FlowStatus.FS2003_CONTRACT_COMPLETED);

		model.addAttribute("data", data);
		model.addAttribute("search", search);
		model.addAttribute("roles", new ViewRoleCtrl(this.getUser(), this.getRoleCodesFromSession()));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contract2UserSrv.getContractCountOf(dc, null, true, FlowStatus.FS2001_CONTRACT_CREATED, FlowStatus.FS2002_CONTRACT_STARTED, FlowStatus.FS2003_CONTRACT_COMPLETED));
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		model.addAttribute("inMoney", inMoney);

		return view;
	}

	@RequestMapping(value = "/ht-click")
	public String sjbAllContract(Contract search, @DateTimeFormat(pattern = "yyyy-M-d") Date start, @DateTimeFormat(pattern = "yyyy-M-d") Date end, Boolean inMoney, String signType, Boolean lxing, String statusName, Integer page, Integer pageSize, Model model) {
		String view = "app/charts/ht-click";

		if (page == null || page < 1)
			page = 1;

		if (pageSize == null || pageSize < 1)
			pageSize = 30;

		DetachedCriteria dc = this.contractSrv.getDc(search);

		dc.add(Restrictions.in("type", ContractType.MAIN_CONTRACT_TYPES));

		if (start != null && end != null) {
			if (Boolean.TRUE.equals(lxing)) {
				// 履行中的合同
				// 统计金额的时候，时间为实际工期
				Criterion startC = Restrictions.and(Restrictions.ge("start", start), Restrictions.le("start", end));
				Criterion endC = Restrictions.and(Restrictions.ge("end", start), Restrictions.le("end", end));

				dc.add(Restrictions.or(startC, endC));
			} else {
				// 统计状态的时候为签订日期
				dc.add(Restrictions.ge("signDate", start));
				dc.add(Restrictions.le("signDate", end));
			}
		}

		if (Boolean.TRUE.equals(inMoney))
			dc.add(Restrictions.in("type", ContractType.IN_MONEY_CONTRACT_TYPE.toArray()));
		else if (Boolean.TRUE.equals(inMoney))
			dc.add(Restrictions.in("type", ContractType.OUT_MONEY_CONTRACT_TYPE.toArray()));

		FlowStatus[] fss = null;
		if (!StringUtils.isBlank(signType)) {
			if ("签订中".equals(signType)) {
				fss = new FlowStatus[] {FlowStatus.FS2000_CONTRACT_TO_CREATE};
			} else if ("已签订".equals(signType)) {
				fss = new FlowStatus[] {FlowStatus.FS2001_CONTRACT_CREATED, FlowStatus.FS2002_CONTRACT_STARTED, FlowStatus.FS2003_CONTRACT_COMPLETED};
			}
		} else if (Boolean.TRUE.equals(lxing)) {
			fss = new FlowStatus[] {FlowStatus.FS2001_CONTRACT_CREATED, FlowStatus.FS2002_CONTRACT_STARTED, FlowStatus.FS2003_CONTRACT_COMPLETED};
		} else {
			fss = new FlowStatus[] {FlowStatus.FS2000_CONTRACT_TO_CREATE, FlowStatus.FS2001_CONTRACT_CREATED, FlowStatus.FS2002_CONTRACT_STARTED, FlowStatus.FS2003_CONTRACT_COMPLETED};
		}

		if (!StringUtils.isBlank(statusName)) {
			ContractStatus status = ContractStatus.getStatusByValue(statusName);

			if (status != null) {
				dc.add(Restrictions.eq("status", status));
				search.setStatus(status);
			}
		}

		List<Contract> data = this.contract2UserSrv.getContractOf(dc, null, true, page, pageSize, fss);

		model.addAttribute("data", data);
		model.addAttribute("search", search);
		model.addAttribute("roles", new ViewRoleCtrl(this.getUser(), this.getRoleCodesFromSession()));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contract2UserSrv.getContractCountOf(dc, null, true, fss));
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		model.addAttribute("inMoney", inMoney);
		model.addAttribute("lxing", lxing);

		return view;
	}

	@RequestMapping(value = "/bid-click")
	public String list(Bid search, @DateTimeFormat(pattern = "yyyy-M-d") Date start, @DateTimeFormat(pattern = "yyyy-M-d") Date end, String statusName, Integer page, Integer pageSize, Model model) {
		String view = "app/charts/bid-click";

		// biddinger.id不能传空值，所以前端传来一个字符串null，这里需要去除
		if (search.getBiddingerInfo() != null && search.getBiddingerInfo().getBiddinger() != null) {
			if ("null".equalsIgnoreCase(search.getBiddingerInfo().getBiddinger().getId()))
				search.getBiddingerInfo().setBiddinger(null);
		}

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		DetachedCriteria dc = this.bidSrv.getDc(search);

		if (start != null && end != null) {
			dc.add(Restrictions.ge("signDate", start));
			dc.add(Restrictions.le("signDate", end));
		}

		if ("投标中".equals(statusName)) {
			dc.add(Restrictions.in("status", new Object[] {BidStatus.B0001_IN_BIDDING, BidStatus.B0002_MISS_REBID}));
		} else if ("中标".equals(statusName)) {
			dc.add(Restrictions.eq("status", BidStatus.B0003_WIN));
			search.setStatus(BidStatus.B0003_WIN);
		} else if ("未中标".equals(statusName)) {
			dc.add(Restrictions.eq("status", BidStatus.B0004_LOSE));
			search.setStatus(BidStatus.B0004_LOSE);
		}

		dc.addOrder(Order.asc("openingTime")); // 开标时间正序

		List<Bid> data = this.bid2UserSrv.getBidOf(dc, null, null, null, page, pageSize);

		model.addAttribute("biddingers", this.getBiddingers());
		model.addAttribute("bidTypes", this.constants.getBidTypes(null));
		model.addAttribute("bidWays", this.constants.getBidWays(null));
		model.addAttribute("search", search);
		model.addAttribute("data", data);
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.bid2UserSrv.getBidCountOf(dc, null, null));
		model.addAttribute("start", start);
		model.addAttribute("end", end);

		return view;
	}

	private List<Biddinger> getBiddingers() {
		Biddinger search = new Biddinger();
		search.setOwner(new OrgEntity());
		search.getOwner().setId(this.getUser().getOrgId());

		List<Biddinger> data = this.biddingerSrv.readByEntity(search);

		return data == null || data.isEmpty() ? null : data;
	}

}
